package com.base.system.controller.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.system.base.http.HttpResult;
import com.base.system.jpa.entity.JpaPermission;
import com.base.system.mybatisPlus.entity.MybatisPermission;
import com.base.system.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "权限区")
@RestController
@RequestMapping("/sys/per")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @PostMapping("/page")
    @ApiOperation("列表")
    @PreAuthorize("hasAuthority('per_list')")
    HttpResult<IPage<MybatisPermission>> page(Page page, @RequestBody MybatisPermission per) {
        return HttpResult.ok(permissionService.page(page, per));
    }

    @PostMapping("/save")
    @ApiOperation("新增")
    @PreAuthorize("hasAuthority('per_add')")
    HttpResult<Void> save(@RequestBody MybatisPermission per) {
        permissionService.save(per);
        return HttpResult.ok();
    }

    @PutMapping("/update")
    @ApiOperation("修改")
    @PreAuthorize("hasAuthority('per_update')")
    HttpResult<Void> update(@RequestBody MybatisPermission per) {
        permissionService.update(per);
        return HttpResult.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    @PreAuthorize("hasAuthority('per_delete')")
    HttpResult<Void> delete(@PathVariable Long id) {
        permissionService.delete(id);
        return HttpResult.ok();
    }

    @GetMapping("/_all")
    @ApiOperation("获取所有权限")
    HttpResult<List<JpaPermission>> all() {
        return HttpResult.ok(permissionService.all());
    }
}
